package com.jason.happy.common.utils.image.strategy.impl;

import android.graphics.Bitmap;
import android.net.Uri;

import com.facebook.common.executors.CallerThreadExecutor;
import com.facebook.common.executors.UiThreadImmediateExecutorService;
import com.facebook.common.references.CloseableReference;
import com.facebook.datasource.DataSource;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.common.ImageDecodeOptions;
import com.facebook.imagepipeline.common.RotationOptions;
import com.facebook.imagepipeline.core.ImagePipeline;
import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber;
import com.facebook.imagepipeline.image.CloseableImage;
import com.facebook.imagepipeline.image.CloseableStaticBitmap;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.jason.happy.common.utils.image.strategy.ImageStrategy;
import com.orhanobut.logger.Logger;

/**
 * Created by hacker on 17-6-27.
 */

public class FrescoImageStrategy implements ImageStrategy {


    @Override
    public void downloadImage(String url) {
        Logger.d("download");
        Uri uri = Uri.parse(url);
        ImageDecodeOptions decodeOptions = ImageDecodeOptions.newBuilder()
                .build();

        ImageRequest imageRequest = ImageRequestBuilder
                .newBuilderWithSource(uri)
                .setImageDecodeOptions(decodeOptions)
                .setRotationOptions(RotationOptions.autoRotate())
                .setLowestPermittedRequestLevel(ImageRequest.RequestLevel.FULL_FETCH)
                .setProgressiveRenderingEnabled(true)
                .build();

        ImagePipeline imagePipeline = Fresco.getImagePipeline();
        DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, this);
        dataSource.subscribe(new BaseBitmapDataSubscriber() {
            @Override
            protected void onNewResultImpl(Bitmap bitmap) {
                Logger.d("download bitmap:"+bitmap);
            }

            @Override
            protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
                Logger.d("download onFailureImpl bitmap:"+dataSource);
            }
        }, CallerThreadExecutor.getInstance());


        try {
            CloseableReference<CloseableImage> imageCloseableReference = dataSource.getResult();
            if (imageCloseableReference != null) {
                try {
                    CloseableImage closeableImage = imageCloseableReference.get();
                    if (closeableImage != null && closeableImage instanceof CloseableStaticBitmap) {
                        CloseableStaticBitmap closeableStaticBitmap = (CloseableStaticBitmap) closeableImage;
                        Bitmap bitmap = closeableStaticBitmap.getUnderlyingBitmap();
                        if (bitmap != null) {
                            //operating
                            Logger.d("download bitmap:"+bitmap);
                        }
                    }
                } finally {
                    CloseableReference.closeSafely(imageCloseableReference);
                }
            }
        } catch (Exception e) {
            Logger.e(e.getMessage());
        } finally {
            dataSource.close();
        }
    }

    @Override
    public void downloadImageToDiskCache(String url) {
        Uri uri = Uri.parse(url);

        ImageRequest request = ImageRequestBuilder.
                newBuilderWithSource(uri)
                .setRotationOptions(RotationOptions.autoRotate())
                .setLowestPermittedRequestLevel(ImageRequest.RequestLevel.FULL_FETCH)
                .setProgressiveRenderingEnabled(false)
                .build();

        ImagePipeline pipeline = Fresco.getImagePipeline();
        pipeline.prefetchToDiskCache(request, this);
    }

    @Override
    public boolean isCacheInDisk(String url) {
        Uri uri = Uri.parse(url);

        ImagePipeline pipeline = Fresco.getImagePipeline();
        return pipeline.isInDiskCacheSync(uri);
    }
}
